package com.stylefeng.guns.modular.pay.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.pay.model.WithdrawApply;
import com.stylefeng.guns.modular.pay.model.WithdrawtAccount;
import com.stylefeng.guns.modular.pay.service.IWithdrawApplyService;
import com.stylefeng.guns.modular.pay.service.IWithdrawtAccountService;
import com.stylefeng.guns.modular.pay.service.impl.IdWorkerService;

/**
 * 提现申请控制器
 *
 * @author fengshuonan
 * @Date 2018-09-11 16:59:56
 */
@Controller
@RequestMapping("/withdrawApply")
public class WithdrawApplyController extends BaseController {

    private String PREFIX = "/pay/withdrawApply/";

    @Autowired
    private IWithdrawApplyService withdrawApplyService;
    @Autowired
    private IWithdrawtAccountService iWithdrawtAccountService;
	@Autowired
	private IdWorkerService idWorkerService;


    /**
     * 跳转到提现申请首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "withdrawApply.html";
    }

    /**
     * 跳转到添加提现申请
     */
    @RequestMapping("/withdrawApply_add")
    public String withdrawApplyAdd(Model model) {
    	//关联帐号
    	List<WithdrawtAccount> accountList = iWithdrawtAccountService.selectList(new WithdrawtAccount());
    	model.addAttribute("accountList",accountList);
        return PREFIX + "withdrawApply_add.html";
    }
    
    /**
     * 跳转到审核
     */
    @RequestMapping("/withdrawApply_audit/{withdrawApplyId}")
    public String withdrawApplyAudit(@PathVariable Integer withdrawApplyId,Model model) {
    	 WithdrawApply withdrawApply = withdrawApplyService.selectById(withdrawApplyId);
         model.addAttribute("item",withdrawApply);
        return PREFIX + "withdrawApply_audit.html";
    }

    /**
     * 跳转到修改提现申请
     */
    @RequestMapping("/withdrawApply_update/{withdrawApplyId}")
    public String withdrawApplyUpdate(@PathVariable Integer withdrawApplyId, Model model) {
        WithdrawApply withdrawApply = withdrawApplyService.selectById(withdrawApplyId);
        model.addAttribute("item",withdrawApply);
        LogObjectHolder.me().set(withdrawApply);
        return PREFIX + "withdrawApply_edit.html";
    }

    /**
     * 获取提现申请列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(WithdrawApply apply) {
     	Page<WithdrawApply> page = new PageFactory<WithdrawApply>().defaultPage();
    	List<WithdrawApply> result = withdrawApplyService.selectList(apply);
        page.setRecords(result);
        return super.packForBT(page);
    }

    /**
     * 新增提现申请
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(WithdrawApply withdrawApply) {
    	WithdrawtAccount account = iWithdrawtAccountService.selectById(withdrawApply.getPkAccountId());
    	withdrawApply.setAccountName(account.getAccountName());
    	withdrawApply.setAccountNo(account.getAccountNo());
    	withdrawApply.setStatus(0);//提交审核中
    	withdrawApply.setCreateTime(new Date());
    	withdrawApply.setApplyDate(new Date());
    	withdrawApply.setChargeFee(2);
    	withdrawApply.setOutTradeNo(idWorkerService.idGenerator());
    	withdrawApplyService.insert(withdrawApply);
        return SUCCESS_TIP;
    }
    
    
    /**
     * 审核
     */
    @RequestMapping(value = "audit")
    @ResponseBody
    public Object audit(WithdrawApply withdrawApply) {
    	try {
    		withdrawApplyService.audit(withdrawApply);
		} catch (Exception e) {
			e.printStackTrace();
			 return new ErrorTip(5000,"未知错误");
		}
        return SUCCESS_TIP;
    }
    
    /**
     * 打款
     */
    @RequestMapping(value = "/remittance")
    @ResponseBody
    public Object remittance(@RequestParam Integer withdrawApplyId) {
        withdrawApplyService.remittance(withdrawApplyId);
        return SUCCESS_TIP;
    }

    /**
     * 删除提现申请
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer withdrawApplyId) {
        withdrawApplyService.deleteById(withdrawApplyId);
        return SUCCESS_TIP;
    }

    /**
     * 修改提现申请
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(WithdrawApply withdrawApply) {
        withdrawApplyService.updateById(withdrawApply);
        return SUCCESS_TIP;
    }

    /**
     * 提现申请详情
     */
    @RequestMapping(value = "/detail/{withdrawApplyId}")
    @ResponseBody
    public Object detail(@PathVariable("withdrawApplyId") Integer withdrawApplyId) {
        return withdrawApplyService.selectById(withdrawApplyId);
    }
}
